package cn.kgc.service;

import cn.kgc.common.CrowdFundingUtils;
import cn.kgc.mapper.HstaffMapper;
import cn.kgc.pojo.Hstaff;
import cn.kgc.pojo.HstaffExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("hstaffService")
public class HstaffServiceImpl implements HstaffService {
    @Resource
    HstaffMapper hstaffMapper;



    @Override
    public PageInfo<Hstaff> queryForKeywordSearchHstaff(Integer pageNum, Integer pageSize, String keyword) {

        PageHelper.startPage(pageNum,pageSize);

        if(CrowdFundingUtils.stringEffective(keyword)){
            HstaffExample example = new HstaffExample();
            HstaffExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike("%"+keyword+"%");
            List<Hstaff> allHousestaffUsername = hstaffMapper.selectByExample(example);
            PageInfo<Hstaff> pageInfo=new PageInfo<>(allHousestaffUsername);
            return pageInfo;
        }else{
            HstaffExample example = new HstaffExample();
            HstaffExample.Criteria criteria = example.createCriteria();
            List<Hstaff> buildings = hstaffMapper.selectByExample(null);
            PageInfo<Hstaff> pageInfo=new PageInfo<>(buildings);
            return pageInfo;
        }
    }

    @Override
    public Hstaff selectByIdHstaffXiang(Integer id) {
        return hstaffMapper.selectByPrimaryKey(id);
    }

    @Override
    public int getUserListByIdList(List<Integer> hstaffIdList) {
        HstaffExample example = new HstaffExample();
        HstaffExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(hstaffIdList);

        return  hstaffMapper.deleteByExample(example);
    }

    @Override
    public void insertHousestaff(Hstaff hstaff) {
        String hstaffpawd = hstaff.getPassword();
        hstaffpawd = CrowdFundingUtils.md5(hstaffpawd);
        hstaff.setPassword(hstaffpawd);
        hstaffMapper.insert(hstaff);

    }

    @Override
    public Hstaff selectByIdHstaffToUpdatge(Integer id) {
        return hstaffMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateHousestaff(Hstaff hstaff) {
        String hstaffPswd = hstaff.getPassword();
        hstaffPswd = CrowdFundingUtils.md5(hstaffPswd);

        hstaff.setPassword(hstaffPswd);
        hstaffMapper.updateByPrimaryKeySelective(hstaff);
    }

    @Override
    public int deleteStaff(int id) {
        return hstaffMapper.deleteByPrimaryKey(id);
    }
}
